package com.mywork.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mywork.dao.CurelistMapper;
import com.mywork.dao.PayDetailsListMapper;

import com.mywork.dao.PayListMapper;
import com.mywork.dao.PayListViewMapper;
import com.mywork.dao.RecipeMapper;
import com.mywork.po.Page;
import com.mywork.po.PayDetailsList;

import com.mywork.po.PayList;
import com.mywork.po.PayListView;


@Service
public class PayService {
	@Autowired
	RecipeMapper recipeDao;
	@Autowired
	PayListMapper payListDao;// 缴费表
	@Autowired
	PayDetailsListMapper payDetailsListDao;
	@Autowired
	CurelistMapper curelistDao;
	@Autowired
	PayListViewMapper payLisViewtDao;

	/**
	 * 分装清单列表信息，并修改处方缴费标记，插入缴费单，插入缴费清单
	 * 
	 * @param payList
	 * @param cureId
	 * @param curePrice
	 * @param cureDiscountPrice
	 * @param cureSpecialDiscountPrice
	 * @param drugId
	 * @param drugPrice
	 * @param drugDiscountPrice
	 * @param drugSpecialDiscountPrice
	 * @param userId 
	 */
	public void pay(PayList payList, String[] cureId, String[] curePrice,
			String[] cureDiscountPrice, String[] cureSpecialDiscountPrice,
			String[] drugId, String[] drugPrice, String[] drugDiscountPrice,
			String[] drugSpecialDiscountPrice, String userId) {
		// TODO 自动生成的方法存根
		payList.setPaytime(new Date());
		payList.setSaveuserid(userId);
		// 计算实收金额是多少
		payList.setTrueamount(payList.getBankcard().add(payList.getCash()).add(payList.getMedicalinsurancecard()));
		// 插入缴费单
		payListDao.insert(payList);

		// 插入缴费清单表
		// 为所有的治疗项目缴费插入缴费清单表
		for (int i = 0; i < cureId.length; i++) {
			PayDetailsList payDetailsList = new PayDetailsList();
			payDetailsList.setCureid(Long.valueOf(cureId[i]).longValue());
			payDetailsList.setDiscountprice(new BigDecimal(cureDiscountPrice[i]).longValue());
			payDetailsList.setPrice(new BigDecimal(curePrice[i]).longValue());
			payDetailsList.setRecipeid(payList.getRecipeid());
			payDetailsList.setSpecialdiscountprice(new BigDecimal(cureSpecialDiscountPrice[i]).longValue());
			String type = getCureType(cureId[i]);
			payDetailsList.setType(type);
			payDetailsListDao.insert(payDetailsList);

		}
		// 为所有的治疗项目插入缴费清单表
		for (int i = 0; i < drugId.length; i++) {
			PayDetailsList payDetailsList = new PayDetailsList();
			payDetailsList.setCureid(Long.valueOf(drugId[i]).longValue());
			payDetailsList.setDiscountprice(new BigDecimal(drugDiscountPrice[i]).longValue());
			payDetailsList.setPrice(new BigDecimal(drugPrice[i]).longValue());
			payDetailsList.setRecipeid(payList.getRecipeid());
			payDetailsList.setSpecialdiscountprice(new BigDecimal(drugSpecialDiscountPrice[i]).longValue()
					);
			payDetailsList.setType("0");
			payDetailsListDao.insert(payDetailsList);
		}

		// 修改处方表的缴费标志
		recipeDao.changeCharge(payList.getRecipeid());
	}

	

	private String getCureType(String cureId) {
		// TODO 自动生成的方法存根
		String cureName=curelistDao.getCureType(cureId);
		if(cureName=="护士理疗类"){
			return "1";
		}else if(cureName=="治疗类"){
			return "2";
		}else if(cureName=="X光"){
			return "3";
		}else if(cureName=="检验"){
			return "4";
		}else if(cureName=="心电图"){
			return "5";
		}else{
			return "0";
		}
		
	}



	public int getCountOfAllPayList() {
		// TODO 自动生成的方法存根
		return payLisViewtDao.getCountOfAllPayList();
	}



	public List<PayListView> getAllPayList(Page page) {
		// TODO 自动生成的方法存根
		return payLisViewtDao.getAllPayList(page.getPageSize()*page.getPageNo(),page.getPageSize());
	}

	public PayListView getPayListByRecipeId(String recipeId) {
		// TODO 自动生成的方法存根
		return payLisViewtDao.getPayListByRecipeId(recipeId);
	}

}
